﻿import QtQuick
import QtQuick.Controls
import QtQuick.Window
import QtQuick.Layouts
import FilUI

Rectangle {
    id: control

    // var
    property string title                  : "FilAppBar"
    property string minimizeText           : qsTr("Minimize")
    property string restoreText            : qsTr("Restore")
    property string maximizeText           : qsTr("Maximize")
    property string closeText              : qsTr("Close")
    property string stayTopText            : qsTr("Sticky on Top")
    property string stayTopCancelText      : qsTr("Sticky on Top cancelled")
    property color textColor               : FilTheme.fontPrimaryColor
    property color staytopNormalColor      : Qt.rgba(0, 0, 0, 0)
    property color staytopHoverColor      : Qt.rgba(0, 0, 0, 0.03)
    property color staytopPressColor      : Qt.rgba(0, 0, 0, 0.06)
    property color minimizeNormalColor     : FilTheme.itemNormalColor
    property color minimizeHoverColor      : FilTheme.itemHoverColor
    property color minimizePressColor      : FilTheme.itemPressColor
    property color maximizeNormalColor     : FilTheme.itemNormalColor
    property color maximizeHoverColor      : FilTheme.itemHoverColor
    property color maximizePressColor      : FilTheme.itemPressColor
    property color closeNormalColor        : Qt.rgba(0, 0, 0, 0)
    property color closeHoverColor         : Qt.rgba(251/255, 115/255, 115/255, 1)
    property color closePressColor         : Qt.rgba(251/255, 115/255, 115/255, 0.8)
    property bool showClose                : true
    property bool showMinimize             : true
    property bool showMaximize             : true
    property bool showStayTop              : true
    property bool titleVisible             : true
    property url icon
    property size iconSize                 : "20x20"
    property color borerlessColor          : FilTheme.primaryColor
    property alias buttonStayTop           : btn_stay_top // 置顶
    property alias buttonMinimize          : btn_minimize // 最小化
    property alias buttonMaximize          : btn_maximize // 最大化
    property alias buttonClose             : btn_close    // 关闭
    property alias layoutStandardbuttons   : layout_standard_buttons
    property Component customCom                          // 自定义按钮组件
    // 最大化按钮监听
    property var maxClickListener          : function() {
        if (d.win.visibility === Window.Maximized || d.win.visibility === Window.FullScreen)
            d.win.showNormal()
        else
            d.win.showMaximized()
        d.hoverMaxBtn = false
    }
    // 最小化按钮监听
    property var minClickListener          : function() {
        if (d.win.transientParent !== null) {
            d.win.transientParent.showMinimized()
        }else{
            d.win.showMinimized()
        }
    }
    // 关闭按钮监听
    property var closeClickListener        : function() {
        d.win.close()
    }
    // 置顶按钮监听
    property var stayTopClickListener      : function() {
        if (d.win instanceof FilWindow) {
            d.win.stayTop = !d.win.stayTop
        }
    }

    // in var
    color                                  : Qt.rgba(0, 0, 0, 0)
    height                                 : visible ? 30 : 0
    opacity                                : visible
    z                                      : 65535

    Item {
        id: d
        property var hitTestList           : []
        property bool hoverMaxBtn          : false
        property var win                   : Window.window
        property bool stayTop              : {
            if (d.win instanceof FilWindow) {
                return d.win.stayTop
            }
            return false
        }
        property bool isRestore            : win && (Window.Maximized === win.visibility ||
                                                     Window.FullScreen === win.visibility)
        property bool resizable            : win && !(win.height === win.maximumHeight &&
                                                      win.height === win.minimumHeight &&
                                                      win.width === win.maximumWidth &&
                                                      win.width === win.minimumWidth)
        function containsPointToItem(point, item) {
            var pos = item.mapToGlobal(0, 0)
            var rect = Qt.rect(pos.x, pos.y, item.width, item.height)
            if (point.x > rect.x &&
                point.x < (rect.x + rect.width) &&
                point.y > rect.y &&
                point.y < (rect.y + rect.height)) {
                return true
            }
            return false
        }
    }

    // 左侧标题块(包含图标及标题)
    Row{
        anchors{
            verticalCenter                  : parent.verticalCenter
            left                            : parent.left
            leftMargin                      : 10
            // horizontalCenter: undefined
        }
        spacing: 10
        // 图标
        Image{
            width                           : control.iconSize.width
            height                          : control.iconSize.height
            visible                         : status === Image.Ready ? true : false
            source                          : control.icon
            anchors.verticalCenter          : parent.verticalCenter
        }
        // 标题
        Text {
            text                            : title
            visible                         : control.titleVisible
            color                           : control.textColor
            anchors.verticalCenter          : parent.verticalCenter
        }
    }

    RowLayout {
        id: layout_standard_buttons
        height                              : parent.height
        anchors.right                       : parent.right
        spacing                             : 0
        // 自定义按钮
        Loader {
            sourceComponent                 : customCom
        }
        // 置顶
        FilIconButton {
            id: btn_stay_top
            Layout.preferredWidth           : 40
            Layout.preferredHeight          : control.height // 30
            padding                         : 0
            verticalPadding                 : 0
            horizontalPadding               : 0
            iconSource                      : FilIcons.Pinned
            Layout.alignment                : Qt.AlignVCenter
            iconSize                        : 14
            visible                         : {
                if (!d.win instanceof FilWindow) {
                    return false
                }
                return showStayTop
            }
            text                            : d.stayTop ? control.stayTopCancelText : control.stayTopText
            radius                          : 0
            iconColor                       : d.stayTop ? FilTheme.primaryColor : control.textColor
            color                           : {
                if (pressed) {
                    return staytopPressColor
                }
                return hovered ? staytopHoverColor : staytopNormalColor
            }
            onClicked                       : stayTopClickListener()
        }
        // 最小化
        FilIconButton {
            id: btn_minimize
            Layout.preferredWidth           : 40
            Layout.preferredHeight          : control.height // 30
            padding                         : 0
            verticalPadding                 : 0
            horizontalPadding               : 0
            // iconSourceUrl                   : "qrc:/res/image/minimize.svg"
            iconSource                      : FilIcons.ChromeMinimize
            Layout.alignment                : Qt.AlignVCenter
            // iconSizeUrl                     : Qt.size(14, 14)
            iconSize                        : 11
            visible                         : showMinimize
            text                            : minimizeText
            radius                          : 0
            iconColor                       : control.textColor
            color                           : {
                if (pressed) {
                    return minimizePressColor
                }
                return hovered ? minimizeHoverColor : minimizeNormalColor
            }
            onClicked                       : minClickListener()
        }
        // 最大化
        FilIconButton {
            id: btn_maximize
            property bool hover             : btn_maximize.hovered
            Layout.preferredWidth           : 40
            Layout.preferredHeight          : control.height // 30
            padding                         : 0
            verticalPadding                 : 0
            horizontalPadding               : 0
            // iconSourceUrl                   : d.isRestore ? "qrc:/res/image/restore.svg" : "qrc:/res/image/maximize.svg"
            iconSource                      : d.isRestore  ? FilIcons.ChromeRestore : FilIcons.ChromeMaximize
            Layout.alignment                : Qt.AlignVCenter
            // iconSizeUrl                     : Qt.size(14, 14)
            iconSize                        : 11
            visible                         : d.resizable && showMaximize
            text                            : d.isRestore ? restoreText : maximizeText
            radius                          : 0
            iconColor                       : control.textColor
            color                           : {
                if (down) {
                    return maximizePressColor
                }
                return btn_maximize.hover ? maximizeHoverColor : maximizeNormalColor
            }
            onClicked                       : maxClickListener()
        }
        // 关闭
        FilIconButton {
            id: btn_close
            Layout.preferredWidth           : 40
            Layout.preferredHeight          : control.height // 30
            padding                         : 0
            verticalPadding                 : 0
            horizontalPadding               : 0
            // iconSourceUrl                   : "qrc:/res/image/close.svg"
            iconSource                      : FilIcons.ChromeClose
            Layout.alignment                : Qt.AlignVCenter
            // iconSizeUrl                     : Qt.size(14, 14)
            iconSize                        : 11
            visible                         : showClose
            text                            : closeText
            radius                          : 0
            iconColor                       : hovered ? Qt.rgba(1, 1, 1, 1) : control.textColor
            color                           : {
                if (pressed) {
                    return closePressColor
                }
                return hovered ? closeHoverColor : closeNormalColor
            }
            onClicked                       : closeClickListener()
        }
    }
}
